import Router from 'koa-router'
import doubanData from '../../static/data.json'
import { myTools } from '../../assets/myTools/jsTools/jsTools'
// import User from '../dbs/module/user'

const router = new Router({ prefix: '/index' })
router.get('/swipe', async (ctx) => {
  let swipe = []
  doubanData.forEach((item, idx) => {
    if (item.isIndexSlider.isIndexSlider) {
      swipe.push(
        {
          product_id: item.product_id,
          img: item.isIndexSlider.m_img
        }
      )
    }
  })
  ctx.body = {
    code: 0,
    data: swipe
  }
})

router.get('/newpro', async (ctx) => {
  let newpro = []
  doubanData.forEach((item, idx) => {
    if (item.isNew.isNew) {
      newpro.push({
        title: item.title,
        desc: item.desc,
        img: item.isNew.m_img,
        price: item.price,
        promote_price: item.promote_price,
        product_id: item.product_id
      })
    }
  })
  ctx.body = {
    code: 0,
    data: newpro
  }
})

router.get('/hotpro', async (ctx) => {
  let hotpro = []
  doubanData.forEach((item, idx) => {
    if (item.isHot.isHot) {
      hotpro.push({
        title: item.title,
        desc: item.desc,
        img: item.isHot.m_img,
        price: item.price,
        promote_price: item.promote_price,
        product_id: item.product_id
      })
    }
  })
  ctx.body = {
    code: 0,
    data: hotpro
  }
})

router.get('/reviews', async (ctx) => {
  let reviews = []
  doubanData.forEach((item, idx) => {
    if(item.p_detail.reviews.length !== 0) {
      reviews.push({
        usrname: item.p_detail.reviews[0].username,
        usrimg: item.p_detail.reviews[0].avatar,
        reviewtxt: item.p_detail.reviews[0].description,
        reviewimg: item.p_detail.reviews[0].html_images[0].raw_src,
        product_id: item.product_id
      })
    }
  })
  ctx.body = {
    code: 0,
    data: reviews.slice(0, 4)
  }
})

export default router
